package com.squareshoot.picCheckin;

import java.util.ArrayList;
import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class VenueMap extends MapActivity {

	MapView map;
	VenueMapOverlay itemizedOverlay;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.venuemap);

		map=(MapView)findViewById(R.id.mapview);

		Bundle extras = getIntent().getExtras();
		double myLatitude = extras.getDouble("myLatitude");
		double myLongitude = extras.getDouble("myLongitude");
		double geolat = extras.getDouble("geolat");
		double geolong = extras.getDouble("geolong");

		map.getController().setCenter(getPoint(geolat,geolong));
		map.getController().setZoom(17);
		map.setBuiltInZoomControls(true);
		
		List<Overlay> mapOverlays = map.getOverlays();
		
		Drawable myMarker = getResources().getDrawable(R.drawable.ic_maps_indicator_current_position);
		myMarker.setBounds(-myMarker.getIntrinsicWidth()/2,-myMarker.getIntrinsicHeight()/2,myMarker.getIntrinsicWidth()/2,myMarker.getIntrinsicHeight()/2);
		
		VenueMapOverlay myItemOverlay = new VenueMapOverlay(myMarker);
		OverlayItem overlayitem = new OverlayItem(getPoint(myLatitude,myLongitude), "Hola, Mundo!", "I'm in Mexico City!");
		myItemOverlay.addOverlay(overlayitem);
		mapOverlays.add(myItemOverlay);
		
		
		Drawable venueMarker = getResources().getDrawable(R.drawable.marker);
		venueMarker.setBounds(-venueMarker.getIntrinsicWidth() / 2,-venueMarker.getIntrinsicHeight(), venueMarker.getIntrinsicWidth() / 2,0);
		
		VenueMapOverlay venueOverlay = new VenueMapOverlay(venueMarker);
		OverlayItem venueItem = new OverlayItem(getPoint(geolat,geolong), "Hola, Mundo!", "I'm in Mexico City!");
		venueOverlay.addOverlay(venueItem);
		mapOverlays.add(venueOverlay);
	}
	
	private GeoPoint getPoint(double lat, double lon) {
		return(new GeoPoint((int)(lat*1000000.0),(int)(lon*1000000.0)));
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	
	private class VenueMapOverlay extends ItemizedOverlay<OverlayItem> {

		private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
		
		public VenueMapOverlay(Drawable marker) {
			super(marker);
		}

		@Override
		protected OverlayItem createItem(int i) {
			return mOverlays.get(i);
		}

		@Override
		public int size() {
			return mOverlays.size();
		}
		
		public void addOverlay(OverlayItem overlay) {
			mOverlays.add(overlay);
			populate();
		}
	}
}
